Imports org.model.lib.db

''' <summary>
''' ΔΑΣ: Country Code/Year of Perido/Student Counter
''' </summary>
''' <remarks></remarks>
Class DASCandidateCodeGenerator
    Inherits ICandidateCodeGenerator


    ''' <summary>
    ''' Rerurns an existing candidate code for THE STUDENT
    ''' </summary>
    Public Overrides Function getExistingCandidateCode() As String

        Dim oldrecords As List(Of ClassMember) = ClassMemberDataUtils.findList( _
                    "studentId={0}", _
                    Me.student.StudentId)

        For Each cm As ClassMember In oldrecords
            If cm.PeriodClass.Period.DiplomaSuperTypeId = EnumDiplomaSuperTypes.PAS Or _
                    cm.PeriodClass.Period.DiplomaSuperTypeId = EnumDiplomaSuperTypes.DAS Then

                Return cm.CandidateCode
            End If
        Next

        Return String.Empty

    End Function

    Public Overrides Function getStudentCounter() As Integer
        Dim sql = <sql>
SELECT  max(convert(int, RIGHT( a.CandidateCode, 4)))
FROM    ClassMember AS a INNER JOIN
        Class AS b ON a.ClassId = b.ClassId INNER JOIN
        Period AS c ON b.PeriodId = c.PeriodId
WHERE   c.DiplomaSuperTypeId in (5,2)
                  </sql>

        Dim ret As Integer = ModelContext.CurrentDBUtils.getLngValueWithParams(sql.Value)
        ret = ret + 1

        Return ret

    End Function

    Public Overrides Function getNewCandidateCode() As String

        'Candidate code:  Country Code/Period Code/Student Counter for Country Code
        Dim candCode As String = Me.periodClass.ExamCenter.Country.CountryCode & "/" & _
                                Me.periodClass.Period.PeriodDate.GetValueOrDefault.Year & "/"

        Dim stCounter As Integer = Me.getStudentCounter

        candCode = candCode & Format(stCounter, "0000")
        Return candCode

    End Function
End Class
